<?php

namespace app\api\service;

use EasyWeChat\Factory;
use think\Exception;

/**
 * 基类服务
 * Class BaseService
 * @package app\admin\service
 */
class BaseService
{
    // 模型
    protected $model;
    // 用户ID
    protected $userId;

    /**
     * 初始化模型
     */
    public function __construct()
    {
        // 获取Token
        $token = request()->header("Authorization");
        if ($token && strpos($token, 'Bearer ') !== false) {
            $token = str_replace("Bearer ", null, $token);
            // JWT解密token
            $jwt = new \Jwt();
            $this->userId = $jwt->verifyToken($token);
        }
    }

    /**
     * JSON返回函数
     *
     */
    public function success($data=[], $message='success')
    {
        return json(['code' => 200, 'data' => $data, 'message' => $message], 200, ['Content-Type' => 'application/json']);
    }

    /**
     * JSON返回函数
     *
     */
    public function error($message='', $code=400)
    {
        return json(['code' => $code, 'data' => [], 'message' => $message], 200, ['Content-Type' => 'application/json']);
    }

}